What does the `new` keyword do

Posted by Mike on Stack Overflow See other posts from Stack Overflow or by Mike
Published on 2012-12-18T04:54:51Z Indexed on 2012/12/18 5:03 UTC
Read the original article Hit count: 117

Filed under:
|

I'm following a Java tutorial online, trying to learn the language, and it's bouncing between two semantics for using arrays.

long results[] = new long[3];
results[0] = 1;
results[1] = 2;
results[2] = 3;

and:

long results[] = {1, 2, 3};

The tutorial never really mentioned why it switched back and forth between the two so I searched a little on the topic. My current understanding is that the new operator is creating an object of "array of longs" type. What I do not understand is why do I want that, and what are the ramifications of that?

  • Are there certain "array" specific methods that won't work on an array unless it's an "array object"?
  • Is there anything that I can't do with an "array object" that I can do with a normal array?
  • Does the Java VM have to do clean up on objects initialized with the new operator that it wouldn't normally have to do?

I'm coming from C, so my Java terminology, may not be correct here, so please ask for clarification if something's not understandable.

© Stack Overflow or respective owner

Related posts about java

Related posts about new-operator